//
//  RecommendViewModel.swift
//  DouYuZB
//
//  Created by James on 2017/10/16.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

class RecommendViewModel {
    lazy var anchorGroup:[AnchorGroup] = [AnchorGroup]()
    lazy var cycleModels:[CycleModel] = [CycleModel]()
    private lazy var bigDataGroup:AnchorGroup = AnchorGroup(dict: [:])
    private lazy var prettyDataGroup:AnchorGroup = AnchorGroup(dict: [:])
}

// MARK: - 发送网络请求
extension RecommendViewModel {
    func requestData(completion:@escaping ()->())  {
        //控制请求的顺序
        let gcdGroup = DispatchGroup()
        gcdGroup.enter()//入组
        
        let nowtime  = Date.getNowTime()
        let para = ["limit":"4","offset":"0","time":nowtime]
        //1.推荐数据
        NetWorkManger.requestData(type: .GET, urlString: "http://capi.douyucdn.cn/api/v1/getbigDataRoom", parameters: ["time":nowtime]) { (result) in
            guard let resultDict = result as? NSDictionary ,
                let dataArray = resultDict["data"] as? [[String:NSObject]]else {
                    return
            }
            
            self.bigDataGroup.tag_name = "热门"
            self.bigDataGroup.icon_url = "home_header_hot"
            for dict in dataArray {
                let anchor = AnchorModel(dict: dict)
                self.bigDataGroup.anchors.append(anchor)
            }
            print("--> 0")
            gcdGroup.leave()//出组
        }
        
        //2.请求颜值数据
        gcdGroup.enter()//入组
        NetWorkManger.requestData(type: .GET, urlString: "http://capi.douyucdn.cn/api/v1/getVerticalRoom", parameters: para) { (result) in
            
            guard let resultDict = result as? NSDictionary ,
                let dataArray = resultDict["data"] as? [[String:NSObject]]else {
                return
            }
            
            //创建组和属性
            self.prettyDataGroup.tag_name = "颜值"
            self.prettyDataGroup.icon_url = "home_header_phone"
            
            for dict in dataArray {
                let anchor = AnchorModel(dict: dict)
                self.prettyDataGroup.anchors.append(anchor)
            }
            print("--> 1 ")
            gcdGroup.leave()//出组
        }
        
        //3.请求后面部分游戏数据   2-12
        gcdGroup.enter()//入组
        NetWorkManger.requestData(type: .GET, urlString: "http://capi.douyucdn.cn/api/v1/getHotCate", parameters: para) { (result) in
            //1.将result 转为字典类型 //2.获取数组
            guard let resultDict = result as? NSDictionary ,
                let dataArray = resultDict["data"] as? [[String:NSObject]]else {
                return
            }

            //3.遍历数组  字典转模型
            for dict in dataArray {
                let group = AnchorGroup(dict: dict)
                self.anchorGroup.append(group)
            }
            print("--> 2-12 ")

            gcdGroup.leave()//出组
        }
        
        //所有的数据请求完毕后进行排序
        gcdGroup.notify(queue: .main) {
            print("所有的数据都请求到")
            self.anchorGroup.insert(self.prettyDataGroup, at: 0)
            self.anchorGroup.insert(self.bigDataGroup, at: 0)
            
            completion()
        }
    }

    func requestCycleData(completion:@escaping ()->())  {
        NetWorkManger.requestData(type: .GET, urlString: "http://www.douyutv.com/api/v1/slide/6", parameters: ["version" : "2.300"]) { (result) in
            
            guard let resultDictionary = result as? [String:NSObject] ,
            let dataArray = resultDictionary["data"] as? [[String:NSObject]]
            else{
                return
            }
            //字典转模型
            for dict in dataArray {
                let cycleModel:CycleModel = CycleModel(dict: dict)
                self.cycleModels.append(cycleModel)
            }
            completion()
        }
    }

}
